package com.lanxiaoye.shoppingSystem.web;

import com.lanxiaoye.shoppingSystem.common.Constants;
import com.lanxiaoye.shoppingSystem.common.result.RegisterCheckResult;
import com.lanxiaoye.shoppingSystem.entity.User;
import com.lanxiaoye.shoppingSystem.service.HomeRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by libing on 2016/12/12.
 */
@Controller
@RequestMapping("/register")
public class HomeRegisterController extends BaseController{

    @Autowired
    private HomeRegisterService homeRegisterService;

    @RequestMapping("/jump")
    public String jumpToRegisterPage() throws Exception {
        //跳转到注册的页面
        return "";
    }

    @RequestMapping("/check_name")
    @ResponseBody
    public RegisterCheckResult checkNameExists(HttpServletRequest request){
        RegisterCheckResult registerCheckResult = new RegisterCheckResult();
        String registerName = request.getParameter("registerName");
        int exist = homeRegisterService.checkExistByName(registerName);
        if(exist == 1){
            registerCheckResult.setCheckResult(Constants.USER_EXIST);
            return registerCheckResult;
        }
        registerCheckResult.setCheckResult(Constants.USER_NOT_EXIST);
        return registerCheckResult;
    }

    @RequestMapping("/check_phone")
    @ResponseBody
    public RegisterCheckResult checkPhoneExists(HttpServletRequest request){
        RegisterCheckResult registerCheckResult = new RegisterCheckResult();
        String registerPhone = request.getParameter("registerPhone");
        int exist = homeRegisterService.checkExistByPhone(registerPhone);
        if(exist == 1){
            registerCheckResult.setCheckResult(Constants.USER_EXIST);
            return registerCheckResult;
        }
        registerCheckResult.setCheckResult(Constants.USER_NOT_EXIST);
        return registerCheckResult;
    }

    @RequestMapping("/check_email")
    @ResponseBody
    public RegisterCheckResult checkEmailExists(HttpServletRequest request){
        RegisterCheckResult registerCheckResult = new RegisterCheckResult();
        String registerEmail = request.getParameter("registerEmail");
        int exist = homeRegisterService.checkExistByEmail(registerEmail);
        if(exist == 1){
            registerCheckResult.setCheckResult(Constants.USER_EXIST);
            return registerCheckResult;
        }
        registerCheckResult.setCheckResult(Constants.USER_NOT_EXIST);
        return registerCheckResult;
    }

    @RequestMapping("/register")
    @ResponseBody
    public RegisterCheckResult register(HttpServletRequest request){
        RegisterCheckResult registerCheckResult = new RegisterCheckResult();
        String registerName = request.getParameter("registerName");
        String registerPhone = request.getParameter("registerPhone");
        String registerEmail = request.getParameter("registerEmail");
//        String registerSex = request.getParameter("registerSex");
        String registerPassword = request.getParameter("registerPassword");
        User user = new User();
        user.setName(registerName);
        user.setPhone(registerPhone);
        user.setEmail(registerEmail);
//        user.setSex(registerSex);
        user.setPassword(registerPassword);
        user.setFreeze("否");
        user.setIntegral(0);
        user.setCreateat(new Date().getTime()+"");
        int index = homeRegisterService.registerUser(user);
        if(index == 1){
            registerCheckResult.setCheckResult(Constants.REGISTER_SUCCEED);
            return registerCheckResult;
        }
        registerCheckResult.setCheckResult(Constants.REGISTER_FAILED);
        return registerCheckResult;
    }

}
